let express=require('express')
let router=express.Router()
let sqlQuery=require('../lcSql')
let jiami=require('../md5')

router.get('/',(req,res)=>{
    res.render('login')
})

//表单校验
router.post('/',async (req,res)=>{
    let username=req.body.username
    let password=req.body.password
    let saved=req.body.checkbox
    //console.log(req.body)
    let sqlStr="select * from users where username=? and password = ?"
    let result=await sqlQuery(sqlStr,[username,jiami.md5(password+jiami.salt)])
    if(result.length!=0){
        //登陆成功，进入首页
        req.session.username=username//保存7天session账户
        if(!saved){
            req.session.cookie.maxAge= 10*60*1000;//
            
        }
      res.redirect('/')//重定向到首页
        
    
    }else{
        //登录失败
        res.render('info',{
            title:"登陆失败",
            content:"账号或密码不正确，即将进入登录页",
            href:"/login",
            hrefTxt:"登录页"
        })
    }
})
module.exports= router